﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace Qetza.TeamFoundation.WorkItemQuery.Admin
{
    /// <summary>
    /// Command to delete a work item query.
    /// </summary>
    public class CommandDeleteWiq : ICommand
    {
        #region ICommand Members

        /// <inheritdoc />
        public string Name
        {
            get { return "delete"; }
        }

        /// <inheritdoc />
        public string HelpText
        {
            get { return ResourceStrings.DeleteWiqHelpText; }
        }

        /// <inheritdoc />
        public IEnumerable<ParameterInfo> AllowedParameters
        {
            get
            {
                return new[] {
                    new ParameterInfo() { 
                        Name = CoreArgumentNames.Collection, 
                        ArgumentName = ResourceStrings.ProjectCollectionArgument, 
                        IsRequired = true, 
                        HelpText = ResourceStrings.ProjectCollectionHelpText
                        },
                    new ParameterInfo() { 
                        Name = CoreArgumentNames.Project, 
                        ArgumentName = ResourceStrings.ProjectArgument, 
                        IsRequired = true, 
                        HelpText = ResourceStrings.ProjectHelpText
                        }, 
                    new ParameterInfo() {
                        Name = CoreArgumentNames.QueryPath,
                        ArgumentName = ResourceStrings.QueryPathArgument,
                        IsRequired = true,
                        HelpText = ResourceStrings.Delete_QueryNameHelpText
                        },
                    new ParameterInfo() {
                        Name = CoreArgumentNames.NoPrompt,
                        IsFlag = true,
                        HelpText = ResourceStrings.NoPromptHelpText
                        }
                    };
            }
        }

        /// <inheritdoc />
        public void Execute(IDictionary<string, string> parameters)
        {
            if (parameters.ContainsKey(CoreArgumentNames.NoPrompt) || 
                Utility.Confirm(string.Format(ResourceStrings.DeleteWiqConfirm, parameters[CoreArgumentNames.QueryPath])))
            {
                // get query
                var queries = TfsUtility
                    .GetWorkItemStore(parameters[CoreArgumentNames.Collection], parameters.ContainsKey(CoreArgumentNames.NoPrompt))
                    .Projects[parameters[CoreArgumentNames.Project]]
                    .QueryHierarchy;

                if (parameters[CoreArgumentNames.QueryPath][0] == '@')
                {
                    using (var file = new System.IO.StreamReader(parameters[CoreArgumentNames.QueryPath].Substring(1)))
                    {
                        var line = default(string);
                        while ((line = file.ReadLine()) != null)
                        {
                            if (!string.IsNullOrEmpty(line))
                            {
                                this.DeleteQuery(line, queries);
                            }
                        }
                    }
                }
                else
                {
                    this.DeleteQuery(parameters[CoreArgumentNames.QueryPath], queries);
                }

                queries.Save();
                Console.WriteLine(ResourceStrings.DeleteWiqCompleted);
            }
        }

        /// <inheritdoc />
        public void ValidateParameters(IDictionary<string, string> parameters)
        {
            return;
        }

        #endregion

        #region Private Helpers

        private void DeleteQuery(string queryName, QueryHierarchy queries)
        {
            var query = queries.Find(queryName);
            if (query != null)
            {
                // delete query
                query.Delete();
            }
        }

        #endregion
    }
}
